package com.shchuangzhi.smartguard.core;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;

import com.elvishew.xlog.XLog;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.NetworkInterface;
import java.security.MessageDigest;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by j_hu on 5/3/17.
 */

public class Util {

    private static String HARDWARE_ADDR = "";

    public static String getMD5(String val) {
        String md5 = getMD5(val.getBytes());
        if (md5 == "") {
            md5 = val;
        }
        return md5;
    }

    public static String getMD5(byte[] val) {
        String encrypted = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(val);
            encrypted = new BigInteger(1, md.digest()).toString(16);
        } catch(Exception e) {
            XLog.e("", e);
        }
        return encrypted;
    }

    public static String getHardwareAddr() {
        if (HARDWARE_ADDR.equals("")) {
            try {
                NetworkInterface mac = NetworkInterface.getByName("wlan0");
                HARDWARE_ADDR = getMD5(mac.getHardwareAddress());
            }catch(Exception e) {
                XLog.e("", e);
            }
        }

        return HARDWARE_ADDR;
    }

    public static String getVersion(Activity activity) {
        String version = "";
        try {
            PackageManager manager = activity.getPackageManager();
            PackageInfo info = manager.getPackageInfo(activity.getPackageName(), 0);
            version = String.format(Consts.TITLE_APP_VERSION, info.versionName, info.versionCode);
         } catch (Exception e) {
            XLog.e("", e);
         }
         return version;
    }

    public static String readAll(InputStream input) throws IOException{
        String result = null;
        InputStreamReader in = null;
        try {
            String line;
            in = new InputStreamReader(input);
            StringBuffer sb = new StringBuffer();
            BufferedReader reader = new BufferedReader(in);
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            result = sb.toString();
        }
        finally {
            if (in != null) {
                in.close();
            }
        }
        return result;
    }

    public static byte[] append(byte[] dest, int destLength, byte[] src, int srcLength) {
        byte[] target = null;
        if (dest != null) {
            target = new byte[srcLength + destLength];
            System.arraycopy(dest, 0, target, 0, destLength);
            System.arraycopy(src, 0, target, destLength, srcLength);
        } else {
            target = new byte[srcLength];
            System.arraycopy(src, 0, target, 0, srcLength);
        }
        return target;
    }

    public static Bitmap convertGreyImg(Bitmap img) {
        int width = img.getWidth();         //获取位图的宽
        int height = img.getHeight();       //获取位图的高

        int []pixels = new int[width * height]; //通过位图的大小创建像素点数组

        img.getPixels(pixels, 0, width, 0, 0, width, height);
        int alpha = 0xFF << 24;
        for(int i = 0; i < height; i++)  {
            for(int j = 0; j < width; j++) {
                int grey = pixels[width * i + j];

                int red = ((grey  & 0x00FF0000 ) >> 16);
                int green = ((grey & 0x0000FF00) >> 8);
                int blue = (grey & 0x000000FF);

                grey = (int)((float) red * 0.3 + (float)green * 0.59 + (float)blue * 0.11);
                grey = alpha | (grey << 16) | (grey << 8) | grey;
                pixels[width * i + j] = grey;
            }
        }
        Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        result.setPixels(pixels, 0, width, 0, 0, width, height);
        return result;
    }
}
